package com.survey.action;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.survey.bean.Ques;
import com.survey.bean.QuesItem;
import com.survey.bean.QuesItemList;
import com.survey.bean.QuesItemListOption;
import com.survey.bean.QuesItemListOptionAnswer;
import com.survey.bean.QuesUser;
import com.survey.util.Constant;
import com.survey.util.DateUtil;
import com.survey.util.FieldUtil;
import com.survey.util.MapUtil;
import com.survey.util.MessageUtil;
import com.survey.util.Page;
import com.survey.util.common.BaseAction;
import com.survey.service.QuesItemListOptionService;
import com.survey.service.QuesItemListService;
import com.survey.service.QuesItemService;
import com.survey.service.QuesService;
import com.survey.service.QuesUserService;

@Controller
@RequestMapping("/sys")
public class QuesUserAction extends BaseAction {
	private String						actionname	= "问卷参与";
	private String						actionclass	= "QuesUser";
	@Autowired
	private QuesUserService				service;
	@Autowired
	private QuesService					quesService;
	@Autowired
	private QuesItemService				quesItemService;
	@Autowired
	private QuesItemListService			quesItemListService;
	@Autowired
	private QuesItemListOptionService	quesItemListOptionService;

	@RequestMapping(value = "/add2QuesUser.do")
	public String add2() {
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addQuesUser";
	}

	@SuppressWarnings({  "rawtypes" })
	@RequestMapping(value = "/getQuesUser.do")
	public String get(int uid) {
		try {
			QuesUser quesUser = service.get(uid);
			Ques temp = quesService.get(quesUser.getQuesId());

			if (temp.getEndDate().compareTo(DateUtil.getCurrentTime()) < 0) {
				putRequestValue("errorMessage", "投票已过期");
				return "getQuesAll";
			}
			request.setAttribute("modifybean", temp);
			request.setAttribute("quesUser", quesUser);

			List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", temp.getId()), "id");
			for (QuesItem item : ilist) {
				List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class, MapUtil.createMap("itemId", item.getId()),
						"id");
				item.setList(llist);
				for (QuesItemList itemList : llist) {
					List olist = quesItemListOptionService.selectAll(QuesItemListOption.class,
							MapUtil.createMap("quesItemListId", itemList.getId()), "id");
					itemList.setList(olist);
				}
			}

			putRequestValue("list", ilist);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyQuesUser";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	//保存投票参与结果
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addQuesUser.do")
	public String addQuesUser(QuesUser bean) {
		try {
			List<QuesItemList> list = new ArrayList<QuesItemList>();
			List<QuesItemListOptionAnswer> anslist = new ArrayList<QuesItemListOptionAnswer>();
			List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", bean.getQuesId()), "id");
			JSONArray jsonary = new JSONArray();
			for (QuesItem item : ilist) {
				List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class, MapUtil.createMap("itemId", item.getId()),
						"id");
				for (QuesItemList itemList : llist) {
					//					List<QuesItemListOption> olist = quesItemListOptionService.selectAll(QuesItemListOption.class,
					//							MapUtil.createMap("quesItemListId", itemList.getId()), "id");
					if (itemList.getTypesel().equals("单选")) {
						String v = request.getParameter("score_" + itemList.getId());
						if (StringUtils.isBlank(v)) {
							MessageUtil.addMessage(request, "请先完成投票所有题目.");
							return ERROR;
						}
						itemList.setUserchose(v);

						JSONObject obj = new JSONObject();
						obj.put("type", "单选");
						obj.put("text", "");
						JSONArray ary = new JSONArray();
						ary.add(v);
						obj.put("options", ary);
						jsonary.add(obj);
					} else if (itemList.getTypesel().equals("多选")) {
						String[] v = request.getParameterValues("score_" + itemList.getId());
						if (v == null || v.length == 0) {
							MessageUtil.addMessage(request, "请先完成投票所有题目.");
							return ERROR;
						}
						itemList.setUserchose(v);

						JSONObject obj = new JSONObject();
						obj.put("type", "多选");
						obj.put("text", "");
						JSONArray ary = new JSONArray();
						for (String tempv : v) {
							ary.add(tempv);
						}
						obj.put("options", ary);
						jsonary.add(obj);
					} else if (itemList.getTypesel().equals("填空")) {
						String v = request.getParameter("score_" + itemList.getId());
						itemList.setUserchose(v);
						if (StringUtils.isBlank(v)) {
							MessageUtil.addMessage(request, "请先完成投票所有题目.");
							return ERROR;
						}
						QuesItemListOptionAnswer ans = new QuesItemListOptionAnswer();
						ans.setAnswer(v);
						ans.setItemListId(itemList.getId());
						anslist.add(ans);

						JSONObject obj = new JSONObject();
						obj.put("type", "填空");
						obj.put("text", v);
						obj.put("options", itemList.getId());
						jsonary.add(obj);
					}

				}
				list.addAll(llist);
			}
			bean.setQresult(jsonary.toJSONString());

			bean.setUserId(getSimpleUser().getId());
			bean.setAddDate(DateUtil.getCurrentTime());
			service.addQuesUser(bean, list, anslist);
			MessageUtil.addMessage(request, "提交成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "提交失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateQuesUser.do")
	public String update(QuesUser bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteQuesUser.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQuesUser.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, QuesUser.class, parmnames, parmvalues);

			parmnames.add("userId");
			parmvalues.add(getSimpleUser().getId());

			Page page = service.selectPage(p, QuesUser.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listQuesUser";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
